package com.liwenfeng.thread;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.spi.InetAddressResolver;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class OptimisticLock {
    private AtomicInteger count = new AtomicInteger();
    private int count1;
    List<Integer> xiaoHongs = new ArrayList<>();
    List<Integer> xiaoMings = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException, IOException {
//        OptimisticLock optimisticLock = new OptimisticLock();
//        optimisticLock.test();
//        optimisticLock.faliwu();

        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost.getHostName());
        System.out.println(Arrays.toString(localHost.getAddress()));
        System.out.println(localHost.getHostAddress());
        System.out.println(localHost.getCanonicalHostName());

        System.out.println("---------------------------------------");
        InetAddress baidu = InetAddress.getByName("www.baidu.com");
        System.out.println(baidu.getHostAddress());
        System.out.println(baidu.getCanonicalHostName());
        System.out.println(baidu.getHostName());

        System.out.println(baidu.isReachable(5000));
    }

    public void test() {
        Runnable runnable = () -> {
            for (int i = 0; i < 100; i++) {
//                String sql1 = "select count from goods where id=#{id}";
//                String sql2 = "update goods set count=#{count-1} where id=#{id} and count=#{count}";
//                int res=0;
//                if (res==1){
//                   submit();
//                }else {
//                    rollback();
//                    i--;
//                }
                int c = count.incrementAndGet();
                System.out.println(Thread.currentThread().getName() + "-修改count===>" + c);
            }
        };

        for (int i = 0; i < 100; i++) {
            new Thread(runnable).start();
        }
    }

    //小红和小明同时发100件礼物，留10件不发，利用多线程模拟分发过程，并统计小红和小明各发多少
    public void faliwu() throws InterruptedException {
        count1 = 100;
        Runnable runnable = () -> {
            System.out.println(Thread.currentThread().getName()+"执行了。。。。");
            while (true) {
                synchronized (this) {
                    if (count1 > 10) {
                        count1--;
                        System.out.println(Thread.currentThread().getName() + "发礼物：" + count1);
                        if (Objects.equals(Thread.currentThread().getName(), "小红")) {
                            xiaoHongs.add(count1);
                        } else {
                            xiaoMings.add(count1);
                        }
                    } else {
                        break;
                    }
                }

                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        };
        Thread xiaoHong = new Thread(runnable, "小红");
        xiaoHong.start();
        Thread xiaoMing = new Thread(runnable, "小明");
        xiaoMing.start();

        //主线程等两个线程跑完
        xiaoHong.join();
        xiaoMing.join();

        System.out.println(xiaoHongs.toString() + xiaoHongs.size());
        System.out.println(xiaoMings.toString() + xiaoMings.size());
    }
}
